package com.yjj.service;



import com.yjj.properties.IPProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class IPService {
    private  Map<String,Integer> ipMap=new HashMap<>();
 /*   @Autowired
    //当前request的注入工作由使用当前的starter的项目来完成
    private  HttpServletRequest request;*/

    @Autowired
    private  IPProperties ipProperties;

    public void count( HttpServletRequest request){
        // 获取ip
        String ip=request.getRemoteAddr();
        // 添加到map中
        ipMap.put(ip,ipMap.getOrDefault(ip,1)+1);
        print();
    }

    //每5秒钟统计一次，将map以json的形式返回给前端
    //@Scheduled(cron = "0/${tools.ip.cycle:5} * * * * ?")
    @Scheduled(cron = "0/#{iPProperties.cycle} * * * * ?")
    public  void print(){
        if(ipProperties.getModel().equalsIgnoreCase("simple")){
            System.out.println("simple model");
            System.out.println(ipMap);
        }else if(ipProperties.getModel().equalsIgnoreCase("detail")){
            System.out.println("detail model");
            ipMap.forEach((k,v)->{
                System.out.println(k+"---- "+v);
            });
        }
    }


}
